layui.config({
	base : "js/"
}).use(['form','layer','jquery','layedit','laydate','upload'],function(){
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		layedit = layui.layedit,
		laydate = layui.laydate,
        upload = layui.upload,
    	$ = layui.jquery;
	//实例化上传文件框
    upload.render({
        elem: '#upload'
        ,url: '/uploadFile' //上传接口
        ,done: function(res){
        	$("#imgBox").html('');
            $("#imgBox").append("<a target='_blank' href='"+res.data.src+"'><img src='" + res.data.src + "'>");
			$("#imgBox").append("<input type='hidden' name='imgUrl' class='imgUrl' value='" + res.data.src + "'>");
        }
    });
    if($("#categoryId").text().length<30){
		//查询信息分类
		$.get("/admin/queryCategoryByType/2",function (msg) {
			if(msg.code == 0){
				var str = "";
				var data = msg.data;
				for(var i=0; i<data.length; i++){
					str += '<option value="'+data[i]['categoryId']+'">'+data[i]['name']+'</option>';
				}
				$("#categoryId").append(str);
				form.render('select');
			}else{
				layer.alert("加载信息分类出错");
			}
		});
    }

	//创建一个编辑器
 	form.on("submit(addLinks)",function(data){

 		//同步编辑器内容到textarea
        editor.sync();
        //debugger;
		//不用编解码？？
		//data.field.content =  html_encode($("#content").val());
		data.field.content =  $("#content").val();
 		$.ajax({
			type : "post",
			url : "/admin/addMessage",
			data : JSON.stringify(data.field),
            contentType : "application/json;charset=utf-8", //必须，告诉服务器这是json
            success : function(resp){
                //layer.close(index);
                if(resp.code == '0'){
                	if(data.field.messageId==''){
                        parent.layer.msg('添加成功', {icon: 1});
					} else {
                        parent.layer.msg('更新成功', {icon: 1});
					}
                    layer.closeAll('iframe');
                }else{
                    layer.msg(resp.msg);
                }
            }
		});

 	})
	
})
